<?php

namespace app\api\service;

use think\Request;

class IdentityService extends Api
{
    protected static $instance;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
    }

    public static function getInstance()
    {
        if (is_null(self::$instance))
        {
            self::$instance = new static();
        }
        return self::$instance;
    }

    /**
     * 判断身份证号码是否正确
     */
    public function isID($identity)
    {
        if (strlen($identity) != 18) {
            return false;
        }
        $identity_body = substr($identity, 0, 17); // 身份证主体(前17位)
        $identity_lastCode = strtoupper(substr($identity, 17, 1)); // 身份证最后一位的验证码
        $factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); // 加权因子
        $code = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'); // 校验码对应值
        $checksum = 0;
        for ($i = 0; $i < strlen($identity_body); $i++) {
            $checksum += substr($identity_body, $i, 1) * $factor[$i];
        }
        // $code[$checksum % 11] 为计算后的最后一位效验码
        if ($code[$checksum % 11] != $identity_lastCode) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 通过身份证号码获取性别 男1 女0
     */
    public function getSex($identity)
    {
        return $sex = (substr($identity, 16, 1) % 2) ? 1 : 2;
    }

    /**
     * 通过身份证号码获取生日
     */
    public function getBirthday($identity)
    {
        $year = ((int) substr($identity, 6, 4)); //取得年份
        $month = ((int) substr($identity, 10, 2)); //取得月份
        $day = ((int) substr($identity, 12, 2)); //取得几号
        // return $birthday = $year . '-' . $month . '-' . $day; // 返回文本
        return mktime(0, 0, 0, $month, $day, $year); // 返回时间戳
    }

    /**
     * 获取年龄
     * @param string $identity
     * @return string|number
     */
    public function getAge($identity)
    {
        $date1 = new \DateTime(substr($identity, 6, 8));
        $date2 = new \DateTime();

        return $date1->diff($date2)->y;
    }

    /**
     * 将数字金额转换成中文大写
     */
    public function num_to_rmb($num, $type = 'c1')
    {
        if ($type == 'c1')
        {
            $c1 = "零壹贰叁肆伍陆柒捌玖";
            $c2 = "分角元拾佰仟万拾佰仟亿";
        }else{
            $c1 = "零一二三四五六七八九";
            $c2 = "分角元拾佰仟万拾佰仟亿";
        }

        $num = round($num, 2);
        $num = $num * 100;
        if (strlen($num) > 10) {
            return "金额太大，请检查";
        }
        $i = 0;
        $c = "";
        while (1) {
            if ($i == 0) {
                $n = substr($num, strlen($num) - 1, 1);
            } else {
                $n = $num % 10;
            }
            $p1 = substr($c1, 3 * $n, 3);
            $p2 = substr($c2, 3 * $i, 3);
            if ($n != '0' || ($n == '0' && ($p2 == '亿' || $p2 == '万' || $p2 == '元'))) {
                $c = $p1 . $p2 . $c;
            } else {
                $c = $p1 . $c;
            }
            $i = $i + 1;
            $num = $num / 10;
            $num = (int)$num;
            if ($num == 0) {
                break;
            }
        }
        $j = 0;
        $slen = strlen($c);
        while ($j < $slen) {
            $m = substr($c, $j, 6);
            if ($m == '零元' || $m == '零万' || $m == '零亿' || $m == '零零') {
                $left = substr($c, 0, $j);
                $right = substr($c, $j + 3);
                $c = $left . $right;
                $j = $j - 3;
                $slen = $slen - 3;
            }
            $j = $j + 3;
        }
        if (substr($c, strlen($c) - 3, 3) == '零') {
            $c = substr($c, 0, strlen($c) - 3);
        }
        if (empty($c)) {
            return "零元";
        } else {
            return $c;
        }
    }
}